package org.abratuhi.kvngroupware.scenario.mptua;

import java.util.List;
import java.util.Properties;

import org.abratuhi.kvngroupware.jokemanager.manager.JokeManager;
import org.abratuhi.kvngroupware.login.manager.LoginManager;
import org.abratuhi.kvngroupware.network.AbstractResponseMessageProceeder;
import org.abratuhi.kvngroupware.network.Client;
import org.abratuhi.kvngroupware.network.Message;
import org.abratuhi.kvngroupware.network.MessageSender;
import org.abratuhi.kvngroupware.scenario.gui.GuiScenario;
import org.abratuhi.kvngroupware.scenario.manager.ScenarioManager;
import org.abratuhi.kvngroupware.scenario.model.Scenario;
import org.abratuhi.kvngroupware.widget.WidgetManager;
import org.jdom.Element;

public class ScenarioResponseMessageProceeder extends AbstractResponseMessageProceeder{

	LoginManager loginmanager;
	ScenarioManager scenariomanager;
	JokeManager jokemanager;
	WidgetManager widgetmanager;

	public ScenarioResponseMessageProceeder(MessageSender sender, Properties locale, LoginManager loginmanager, ScenarioManager scenariomanager, JokeManager jokemanager, WidgetManager widgetmanager){
		super(sender, locale);
		this.loginmanager = loginmanager;
		this.scenariomanager = scenariomanager;
		this.jokemanager = jokemanager;
		this.widgetmanager = widgetmanager;
	}

	@Override
	public void proceedMessage(Client client, Message message) {
		Element response = ((Element) message.getRoot().getChildren("response").get(0));
		String type = response.getAttributeValue("type");
		String action = response.getAttributeValue("action");

		if(type.equals("scenario")){
			String teamId = response.getAttributeValue("teamId");
			String userId = response.getAttributeValue("userId");
			String scenarioId = response.getAttributeValue("scenarioId");
			String status = response.getAttributeValue("status");
			Scenario s = (response.getChildren().size() > 0) ? Scenario.fromElement(response.getChild("scenario")) : null;
			
			if(status.equals("ok")){
				if(action.equals("create")){
					scenariomanager.put(s.getId(), s);
					if(widgetmanager.get("scenariolist") != null) widgetmanager.get("scenariolist").repaint();
				}
				else if(action.equals("get")){
					List<Element> scenarios = response.getChildren("scenario");
					scenariomanager.clear();
					for(int i=0; i<scenarios.size(); i++){
						Scenario scenario = Scenario.fromElement(scenarios.get(i));
						scenariomanager.put(scenario.getId(), scenario);
					}
					if(widgetmanager.get("scenariolist") != null) widgetmanager.get("scenariolist").repaint();
				}
				else if(action.equals("join")){
					if(userId.equals(loginmanager.getUser().getId())){
						GuiScenario guiscenario = new GuiScenario(scenarioId, "Scenario: "+s.getTheme(), sender, locale, loginmanager, jokemanager, scenariomanager);
						guiscenario.createWindow();
						widgetmanager.put(s.getId(), guiscenario);
					}
				}
				else if(action.equals("leave")){
					if(userId.equals(loginmanager.getUser().getId())){
						GuiScenario guiscenario = (GuiScenario) widgetmanager.get(s.getId());
						if(guiscenario != null){
							guiscenario.deleteWindow();
							widgetmanager.remove(s.getId());
						}
					}
				}
				else if(action.equals("update")){
					scenariomanager.update(scenarioId, s);
				}
				else if(action.equals("delete")){
					scenariomanager.remove(scenarioId);
					if(widgetmanager.get("scenariolist") != null) widgetmanager.get("scenariolist").repaint();
					GuiScenario guiscenario = (GuiScenario) widgetmanager.get(s.getId());
					if(guiscenario != null){
						guiscenario.deleteWindow();
						widgetmanager.remove(s.getId());
					}
				}
			}
		}
	}
}
